package com.pirestupppai.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseFragment;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.activity.FollowListActivity;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.widgets.dialog.ImageViewerDialog;
import com.pirestupppai.widgets.view.AvatarView;
import com.pirestupppai.widgets.view.FollowButton;

import butterknife.BindView;

import static com.pirestupppai.rxbus.RxBus.RXBUS_REFRESH_USER_DESC;
import static com.pirestupppai.rxbus.RxBus.RXBUS_REFRESH_USER_NAME;
import static com.pirestupppai.ui.activity.FollowListActivity.FOLLOW_TYPE_FANS;
import static com.pirestupppai.ui.activity.FollowListActivity.FOLLOW_TYPE_FOLLOW;
import static com.pirestupppai.widgets.view.FollowButton.REQUEST_UNFOLLOW;

/**
 * author: xiaoluo
 * date: 2017/6/23 10:08
 */
public class UserInfoFragment extends BaseFragment {

    @BindView(R.id.follow_btn)
    FollowButton followBtn;
    @BindView(R.id.fans_count)
    TextView fansCountTv;
    @BindView(R.id.focus_count)
    TextView focusCountTv;
    @BindView(R.id.like_count)
    TextView likeCoutTv;
    @BindView(R.id.nickname_tv)
    TextView nickNameTv;
    @BindView(R.id.user_avatar)
    AvatarView avatarIv;
    @BindView(R.id.backdrop)
    ImageView backIv;
    @BindView(R.id.nickname_vip)
    ImageView nicknameVipIv;
    @BindView(R.id.avatar_layout)
    RelativeLayout avatarLayout;
    @BindView(R.id.follow_layout)
    RelativeLayout followLayout;
    @BindView(R.id.fans_layout)
    RelativeLayout fansLayout;
    @BindView(R.id.like_layout)
    RelativeLayout likeLayout;

    private String mAvatarUrl;
    private int mUid = -1;

    public UserInfoFragment() {
    }

    @SuppressLint("ValidFragment")
    public UserInfoFragment(int uid) {
        mUid = uid;
    }

    @Override
    protected Context getFragmentContext() {
        return getActivity();
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.fragment_user_info;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        avatarIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageViewerDialog dialog = new ImageViewerDialog(mContext, mAvatarUrl);
                dialog.show();
            }
        });

        followLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FollowListActivity.class);
                intent.putExtra(Constant.IntentKey.FOLLOW_LIST_TYPE, FOLLOW_TYPE_FOLLOW);
                intent.putExtra(Constant.IntentKey.UID, mUid);
                startActivity(intent);
            }
        });

        fansLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FollowListActivity.class);
                intent.putExtra(Constant.IntentKey.FOLLOW_LIST_TYPE, FOLLOW_TYPE_FANS);
                intent.putExtra(Constant.IntentKey.UID, mUid);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initLogic() {
        getUserData();
    }


    private void getUserData() {
        NetworkRequest.getInstance().getUserData(mUid, mContext, new TupaiSubscriber<TupaiUser>(mContext) {
            @Override
            protected void requestSuccess(TupaiUser user) {
                setDataView(user);
            }

            @Override
            protected void requestError(String error) {

            }
        });
    }

    /**
     * 根据数据设置视图
     */
    private void setDataView(final TupaiUser user) {
        // 发送RX消息
        RxBus.getInstance().post(RXBUS_REFRESH_USER_DESC, user.getSignature());
        RxBus.getInstance().post(RXBUS_REFRESH_USER_NAME, user.getNickname());

        mAvatarUrl = user.getAvatar();
        mUid = user.getId();
        avatarLayout.setVisibility(View.VISIBLE);
        avatarIv.setTag(R.id.image_url, user.getAvatar());
        avatarIv.setUid(user.getId(), false);
        ImageLoadUtil.getInstance().loadAvatar(user.getAvatar(), avatarIv);
        nickNameTv.setText(user.getNickname());
        focusCountTv.setText(user.getFollowed_count() + "");
        fansCountTv.setText(user.getFollower_count() + "");
        likeCoutTv.setText(user.getLiked_count() + "");
        nicknameVipIv.setVisibility(View.GONE);

        followBtn.setUser(user);
        followBtn.setOnResultListener(new FollowButton.onResultListener() {
            @Override
            public void onSucceed(int type) {
                if (type == REQUEST_UNFOLLOW) {
                    user.setFollower_count(user.getFollower_count() - 1);
                } else {
                    user.setFollower_count(user.getFollower_count() + 1);
                }
                fansCountTv.setText(user.getFollower_count() + "");
            }
        });
    }
}
